package com.tsll.controller;

import com.tsll.annotation.MyLog;
import com.tsll.entity.Expenditure;
import com.tsll.entity.PurchaseBookInfo;
import com.tsll.entity.PurchaseInfo;
import com.tsll.entity.Staff;
import com.tsll.service.ExpenditureService;
import com.tsll.service.PurchaseBookInfoService;
import com.tsll.service.PurchaseInfoService;
import com.tsll.vo.PurchaseFormVo;
import com.tsll.vo.PurchaseInfoVo;
import com.tsll.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author: Tian
 * @version: 1.0
 * @since: 1.0
 * @date: 2024/3/22/17:04
 */
@RestController
@RequestMapping("/purchaseInfo")
public class PurchaseInfoController {

    @Autowired
    PurchaseInfoService purchaseInfoServiceImpl;

    @Autowired
    PurchaseBookInfoService purchaseBookInfoServiceImpl;
    @Autowired
    HttpSession session;
    @Autowired
    ExpenditureService expenditureServiceImpl;

    /**
     * 查询采购信息
     * @return
     */
    @MyLog(businessName = "查询采购信息")
    @RequestMapping("/getAll/{current}/{pageSize}")
    public Result getAll(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody PurchaseInfoVo purchaseInfoVo){
        return purchaseInfoServiceImpl.selectAll( current,pageSize,purchaseInfoVo);
    }

    /**
     * 添加采购信息
     */
    @MyLog(businessName = "添加采购信息")
    @RequestMapping("/addPurchaseInfo")
    @Transactional
    public Result insertPurchaseInfo(@RequestBody PurchaseFormVo purchaseFormVo ) {
        Staff staff = (Staff) session.getAttribute("staff");
        purchaseInfoServiceImpl.insertPurchaseInfo(purchaseFormVo, staff.getId());
        purchaseBookInfoServiceImpl.addBatch(purchaseFormVo.getBookList(),purchaseFormVo.getId());
        return new Result(200,"成功提交了采购申请",null);
    }

    /**
     * 根据采购申请id删除信息
     */
    @MyLog(businessName = "删除采购信息")
    @RequestMapping("/deletePurchaseInfoId/{id}")
    public Result deletePurchaseInfoId(@PathVariable Integer id){
        purchaseBookInfoServiceImpl.deleteByPurchaseinfoId(id);
        purchaseInfoServiceImpl.deletePurchaseInfoId(id);
        return new Result(200,"成功删除采购信息",null);
    }

    /**
     * 查询未审核的信息
     * @param current
     * @param pageSize
     * @param purchaseInfoVo
     * @return
     */
    @MyLog(businessName = "查询未审核的信息")
    @RequestMapping("/selectByExamineStatus/{current}/{pageSize}")
    public Result  selectByExamineStatus(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody PurchaseInfoVo purchaseInfoVo){
        return purchaseInfoServiceImpl.selectByExamineStatus(current,pageSize,purchaseInfoVo);
    }

    /**
     * 根据id修改审核状态
     * @param id
     * @param examinestatus
     * @return
     */
    @MyLog(businessName = "修改审核状态")
    @RequestMapping("/updateExamineStatus/{id}/{examinestatus}")
    @Transactional
    public Result  updateExamineStatus(@PathVariable Integer id,@PathVariable Integer examinestatus){
        List<PurchaseBookInfo> purchaseBookInfos = purchaseBookInfoServiceImpl.selectByPurchaseinfoId(id);
        Double sum=0.0;
        for (PurchaseBookInfo purchaseBook:purchaseBookInfos) {
            sum+=purchaseBook.getBooksCount()*purchaseBook.getBookPrice();
        }
        Staff staff = (Staff) session.getAttribute("staff");

        Expenditure e = new Expenditure(null, sum, "购买图书", new Date(), staff.getDeptid());
        expenditureServiceImpl.insertExpenditure(e);
        return purchaseInfoServiceImpl.updateExamineStatus(id,examinestatus);

    }

    /**
     * 驳回申请
     * @param purchaseInfo
     * @return
     */
    @MyLog(businessName = "驳回申请")
    @RequestMapping("/updateReject")
    public Result updateReject(@RequestBody  PurchaseInfo purchaseInfo){
        return purchaseInfoServiceImpl.updateReject(purchaseInfo);
    }


    /**
     * 购买图书
     * @param id
     * @return
     */
    @RequestMapping("/purchaseBook/{id}")
    public  Result purchaseBook(@PathVariable Integer id){
        return purchaseInfoServiceImpl.purchaseBook(id);
    }

}
